#include "custom_logic_handle.h"
static LIGHT_EFFECT_T light_effect_temporary_config;

void mode_custom_handle()
{
    if ((get_custom_ssp() == (u8)0xFF)&&(get_custom_last_ssp() != get_custom_ssp())) //退出
    {
        printf("33333333\n");
        set_custom_last_ssp(get_custom_ssp());
        if(get_custom_recssp() !=0) //表示没进入过其他模式
        {   
            printf("111111\n");
            set_custom_ssp(get_custom_recssp());
        }
        else if(get_custom_recssp() ==0) //进过其他模式
        {
            printf("222222\n");
            app_logic_set_ssp(app_logic_get_recover_ssp()); //恢复到其他模式
        }
    }
    else if ((get_custom_ssp()==0x00) &&(get_custom_last_ssp() != get_custom_ssp())) //测试模式
    {
        printf("444444\n");
        set_custom_last_ssp(get_custom_ssp());
    }
    else if((get_custom_last_ssp() != get_custom_ssp()) && (get_custom_ssp() != 0xFF)&&(get_custom_ssp()!=0x00))
    {
        printf("5555\n");
        set_custom_last_ssp(get_custom_ssp());
        set_custom_recssp(get_custom_ssp()); //保存恢复状态
        custom_read_partition(get_custom_ssp(),&light_effect_temporary_config);
        set_light_effect_config(light_effect_temporary_config.light_effect_ssp,light_effect_temporary_config.wait,light_effect_temporary_config.len*3,light_effect_temporary_config.buf);
    }

    u8 buf[BUF_SIZE];
    bzero(buf,BUF_SIZE);
    get_cache_data(buf);
    logic_send_light_msg(buf,get_color_length());

}


void custom_logic_handle(SYS_OPT_KEY opt)
{
    static int time;
    if (app_logic_get_last_ssp() != app_logic_get_ssp()) //第一次进入
    {
        LOG_INFO("custom_logic_handle\n");
        app_logic_set_recover_ssp(app_logic_get_last_ssp()); //记录恢复数据
        app_logic_set_last_ssp(app_logic_get_ssp());
        disp_clear_light();
        time = arch_os_ms_now();
    }

    if(arch_os_ms_elapsed(time)>=(10)) //10ms轮询一次
    {
        mode_custom_handle(); //显示部分
        time = arch_os_ms_now();
    }

    if(opt.oepation == OPT_POWER_TURNOFF) //关机
    {
        app_logic_set_ssp(SSP_POWER_OFF);
    }
}